package it.unical.mat.aec.processTests;

import it.unical.mat.aec.model.Test;
import it.unical.mat.aec.model.Traccia;
import it.unical.mat.aec.persistence.GestoreDb;
import it.unical.mat.aec.persistence.PersistanceException;
import it.unical.mat.aec.persistence.TestFileManagerSingleton;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class ProcessTestsFacade {
	
	
	private static ProcessTestsFacade processTestsFacade;
	
	public static ProcessTestsFacade getInstance(){
		if(processTestsFacade==null)
			processTestsFacade=new ProcessTestsFacade();
		return processTestsFacade;
	}
	
	private ArrayList<Test> tests;
	
	private ProcessTestsFacade() {
		tests=new ArrayList<Test>();
	}
	
	public void acquisciEsami(File []files,TipoInputFile tipo) throws LettoreFileTestException, FileNonValidoException {
		BuilderTests builderTests = null;
		try {
			builderTests = new BuilderTests(tipo);
			DirectorTests directorTests=new DirectorTests();
			directorTests.configuraBuilderTests(builderTests);
			directorTests.caricaTests(files);
			tests.addAll(builderTests.getResult());
		} catch (LettoreFileTestException e) {
			if(builderTests!=null)
				tests=builderTests.getResult();
			throw new LettoreFileTestException(e.getMessage());
		} catch (FileNonValidoException e) {
			throw new FileNonValidoException(e.getMessage());
		}
	}
	
	public void associaTraccia(int idTraccia) throws PersistanceException{
		GestoreDb gestoreDb=new GestoreDb();
		Traccia traccia=gestoreDb.getTraccia(idTraccia);
		for(Test t:tests){
			t.setTraccia(traccia);
		}
		
	}
	
	public Traccia getTracciaDaAssociare() throws AssociazioneTracciaException{
		if(tests.size()==0)
			throw new AssociazioneTracciaException("Test not found");
		int idTraccia=tests.get(0).getIdTraccia();
		for(Test t:tests){
			if(t.getIdTraccia()!=idTraccia)
				throw new AssociazioneTracciaException("Test with different track");
		}
		GestoreDb db=new GestoreDb();
		try {
			Traccia traccia=db.getTraccia(idTraccia);
			if(traccia==null)
				throw new AssociazioneTracciaException("Track with id "+idTraccia +" not found");
			return traccia;
		} catch (PersistanceException e) {
			throw new AssociazioneTracciaException("Track with id "+idTraccia +" not found");
		}
	}
	
	public HashMap<Integer, Traccia> getTracce() throws PersistanceException{
		GestoreDb gestoreDb=new GestoreDb();
		return gestoreDb.getTracce();
		
	}
	
	public void correggiTest() throws CorrezioneException{
		Correzione.correggi(tests);
	}
	
	public boolean salvaTests(String path){
		try {
			TestFileManagerSingleton.getInstance().salvaTests(path, tests);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/*	public void caricaTraccia(File []files,TipoInputFile tipo) {
	    BuilderTests builderTests = null;
		try {
			builderTests = new BuilderTests(tipo);
			DirectorTests directorTests=new DirectorTests();
			directorTests.configuraBuilderTests(builderTests);
			directorTests.caricaTests(files);
			tests.addAll(builderTests.getResult());
		} catch (LettoreFileTestException e) {
			if(builderTests!=null)
				tests=builderTests.getResult();
			throw new LettoreFileTestException(e.getMessage());
		} catch (FileNonValidoException e) {
			throw new FileNonValidoException(e.getMessage());
		}
	}*/

	
	public ArrayList<Test> getTests(){
		return tests;
	}

}
